//1 滚动函数
function scroll(){
	if(window.pageXOffset  !== null){
		return{
			left:window.pageXOffset,
			top:window.pageYOffset
		}
			}
		else if(document.compatMode === 'CSS1Compat'){
			return{
				left: document.documentElement.scrollLeft,
				top:document.documentElement.scrollTop
			}
				}
				return{
					top:document.body.scrollTop,
					left:document.body.scrollLeft
				}
	
}
function $(id){
    return typeof id === "string" ? document.getElementById(id) : null;
}
function show(obj) {
    return obj.style.display = 'block';
}

function hide(obj) {
    return obj.style.display = '';
}
// 通用函数获取屏幕发宽度和高度

function client(){
	if(window.innerWidth){
		return{
			width:window.innerWidth,
			height:window.innerHeight
		}
	}else if(document.compatMode ==='CSS1Compat'){
		return{
			width:document.documentElement.clientWidth,
			height:document.documentElement.clientHeight
		}
	}
	return{
		width:document.body.clientWidth,
		height:document.body.clientHeight
	}
	
}

/*
	//阻止冒泡
	if(event && event.stopPropagation){
					event.stopPropagation();
				}else{
					window.event.cancelBubble = true;
				}*/
				
//基本动画封装
function movex(obj,end ,speed){
	
	clearInterval(obj.timer);
	 obj.timer = setInterval(function(){
		 
		 var len  = obj.offsetLeft > end ? -speed:speed;
		 obj.style.left = obj.offsetLeft + len+'px';
		 
		 if(Math.abs(obj.offsetLeft-end)<Math.abs(len)){
			 clearInterval(obj.timer);
			 obj.style.left =end +'px';
		 }
		 
	 },20);
	
}
//获得css属性兼容函数
function getCSSAttrValue(obj,attr){
	if(obj.currentStyle){
		return obj.currentStyle[attr];
	}else{
		
		return window.getComputedStyle(obj,null)[attr];
	}
	
}

/*
//封装动画函数的最终版本
function buffer(obj,json,fn){
				var begin = 0,end =0, speed =0;
			obj.timer = setInterval(function (){
					//找到一个可用的标志
					var joke = false;
				for(var k in json){
					//透明度
					if("opacity"===k){
						begin = Math.round(parseFloat(getCSSAttrValue(obj,k))*100)||100;
						end = parseInt(json[k]*100);
					}else{
						begin = parseInt(getCSSAttrValue(obj,k))||0
						end = parseInt(json[k]);
					}
					
					
					speed = (end-begin)*0.2;
				
					speed = (end >begin) ? Math.ceil(speed):Math.floor(speed);
					if("opacity"===k){//兼容写法
					obj.style.opacity = (begin + speed)/100;
					obj.style.filter = 'alpha(opacity:'+(begin+speed)+')';
						
					}else{
						obj.style[k] = begin + speed +'px';
					}
					
					
					
					if(begin === end ){
						joke =true;
					}
				}	
					
					if(joke){
						clearInterval(obj.timer);
						console.log(fn);
						if(fn){
							fn();
						}
					}	
				}	,200);	
			
			}
			*/
			
			
			
			function buffer(obj, json, fn) {
				 
			    // 1.1 清除定时器
			    clearInterval(obj.timer);
			
			    // 1.2 设置定时器
			    var begin = 0, target = 0, speed = 0;
			    obj.timer = setInterval(function () {
			        // 1.3.0 旗帜
			        var flag = true;
			        for(var k in json){
			            // 1.3 获取初始值
			            if("opacity" === k){ // 透明度
			                begin =  Math.round(parseFloat(getCSSAttrValue(obj, k)) * 100) || 100;
			                target = parseInt(json[k] * 100);
			            }else if("scrollTop" === k){
			                begin = Math.ceil(obj.scrollTop);
			                target = parseInt(json[k]);
			            }else { // 其他情况
			                begin = parseInt(getCSSAttrValue(obj, k)) || 0;
			                target = parseInt(json[k]);
			            }
			
			            // 1.4 求出步长
			            speed = (target - begin) * 0.2;
			
			            // 1.5 判断是否向上取整
			            speed = (target > begin) ? Math.ceil(speed) : Math.floor(speed);
			
			            // 1.6 动起来
			            if("opacity" === k){ // 透明度
			                // w3c的浏览器
			                obj.style.opacity = (begin + speed) / 100;
			                // ie 浏览器
			                obj.style.filter = 'alpha(opacity:' + (begin + speed) +')';
			            }else if("scrollTop" === k){
			                obj.scrollTop = begin + speed;
			            }else {
			                obj.style[k] = begin + speed + "px";
			            }
			
			            console.log(begin, target);
			
			            // 1.5 判断
			            if(begin !== target){
			                flag = false;
			            }
			        }
			
			        // 1.3 清除定时器
			        if(flag){
			            clearInterval(obj.timer);
			
			            console.log(fn);
			
			            // 判断有没有回调函数
			            if(fn){
			                fn();
			            }
			        }
			    }, 20);
			} 